import java.util.Arrays;
import java.util.Random;
import java.util.Stack;

public class Sort {
    public static void testInsertSort1(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        Sort.insertSort2(array);
        long end = System.currentTimeMillis();
        System.out.println("插入排序swap耗时参考："+(end-start));
    }
    public static void testInsertSort2(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        Sort.insertSort1(array);
        long end = System.currentTimeMillis();
        System.out.println("插入排序耗时参考："+(end-start));
    }

    public static void testShellSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        Sort.shellSort(array);
        long end = System.currentTimeMillis();
        System.out.println("希尔排序耗时参考："+(end-start));

    }

    public static void readyDataOrder(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = i;
            //array[i] = array.length-i;
        }
    }


    public static void readyDataNotOrder(int[] array) {
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100000);
        }
    }
    //直接插入排序
        public static void insertSort2(int[] array) {
        for (int i = 1; i < array.length; i++) {
            for (int j = i-1; j >= 0; j--) {
                if(array[j] > array[j+1]) {
                    swap(array,j+1,j);
                } else {
                    //因为前面已经有序，大于之后直接跳出循环
                    break;
                }
            }
        }
    }
    public static void insertSort1(int[] array) {
        //遍历整个数组
        for (int i = 1; i < array.length; i++) {
            //使0-i有序
            int j = i-1;
            int tmp = array[i];
            for (; j >= 0; j--) {
                if(tmp < array[j]) {
                    array[j+1] = array[j];
                }else {
                    //因为前面已经有序，大于之后直接跳出循环
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    //希尔排序
    public static void shellSort(int[] array) {
        int gap = array.length;
        while(gap != 0) {
            gap /= 2;
            shell1(array,gap);
        }
    }
    public static void shell1(int[] array, int gap) {
        for (int i = 0; i < gap; i++) {
            for (int j = gap; j < array.length; j++) {
                if(array[j-gap] > array[j]) {
                    swap(array,j-gap,j);
                }
            }
        }
    }
public static void shell2(int[] array,int gap) {
    //遍历整个数组
    for (int i = gap; i < array.length; i++) {
        //使0-i有序
        int j = i-gap;
        int tmp = array[i];
        for (; j >= 0; j-=gap) {
            if(tmp < array[j]) {
                array[j+gap] = array[j];
            }else {
                //因为前面已经有序，大于之后直接跳出循环
                break;
            }
        }
        array[j+gap] = tmp;
    }
}
    public static void swap(int[] array,int i,int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            //找到i之后的 最小的 小于array[i]的元素
            for (int j = i; j < array.length; j++) {
                if(array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            //将最小的元素换到i下标处
            swap(array,i,minIndex);
        }
    }

    public static void selectSort2(int[] array) {
       int left = 0;
       int right = array.length-1;
       while(left < right) {
           //两个都初始化在最左边，防止漏掉左边
           int minIndex = left;
           int maxIndex = left;
           //找到中间的最大值与最小值
           for (int i = left+1; i < right; i++) {
               if(array[i] < array[minIndex]) {
                   minIndex = i;
               }
               if(array[i] > array[maxIndex]) {
                   maxIndex = i;
               }
           }
           swap(array,left,minIndex);
           //为了防止最大值在left处，然后被交换到minIndex处
           if(maxIndex == left) {
               maxIndex = minIndex;
           }
           swap(array,right,maxIndex);
           left++;
           right--;
       }
    }
    //排升序要建大堆，排降序建小堆
    public static void heapSort(int[] array) {
        createHeap(array);
        //换成降序
        int end = array.length-1;
        while(end > 0) {
            swap(array,0,end);
            //找到最大值放在最后，最终形成降序
            siftDown(array,0,end);
            end--;
        }
    }
    //创建大根堆（从下面到上面排序，保证下面先形成  大根堆）
    public static void createHeap(int[] array) {
        for(int parent = (array.length-1-1)/2; parent >= 0; parent--) {
            siftDown(array,parent,array.length);
        }
    }
    //向下排序
    public static void siftDown(int[] array,int parent,int len) {
        //孩子节点
        int child = 2*parent + 1;
        //将该父亲节点对应的孩子节点  及  孩子的孩子节点  排序
        while(child < len) {
            //寻找左右孩子节点Max
            if(child + 1 < len && array[child] < array[child+1]) {
                child++;
            }
            if(array[child] > array[parent]) {
                //上大下小，大根堆
                swap(array,child,parent);
                //继续向下排序
                parent = child;
                child = 2*parent+1;
            }else {
                //下面已经排好了，不满足直接返回
                break;
            }
        }
    }
    public static void heapSort2(int[] array) {
        for(int parent = (array.length-1-1)/2; parent >= 0; parent--) {
            siftDown2(array,parent,array.length);
        }
    }
    public static void siftDown2(int[] array,int parent,int len) {
        //孩子节点
        int child = 2*parent + 1;
        //将该父亲节点对应的孩子节点  及  孩子的孩子节点  排序
        while(child < len) {
            //寻找左右孩子节点Min
            if(child + 1 < len && array[child] > array[child+1]) {
                child++;
            }
            if(array[child] < array[parent]) {
                swap(array,child,parent);
                //继续向下排序
                parent = child;
                child = 2*parent+1;
            }else {
                //下面已经排好了，不满足直接返回
                break;
            }
        }
    }

    /**
     * 冒泡排序
     * 时间复杂度：O(N^2)
     *     以下代码 如果程序有序 最好情况下 复杂度为O(N)
     * 空间复杂度：O(1)
     * 稳定性：稳定的排序
     * @param array
     */
    public static void bubbleSort(int[] array) {
        //  i  代表趟数
        for (int i = 0; i < array.length-1; i++) {
            boolean tmp = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] > array[j+1]) {
                    swap(array,j,j+1);
                    tmp = true;
                }
            }
            //只要互换一次tmp = true   否则数组已经有序
            if(!tmp) {
                break;
            }
        }
    }

    public static void quickSort(int[] array) {
        //递归需要三个参数
        quick(array,0,array.length-1);
    }



    public static void quick(int[] array,int left,int right) {
        if(left >= right) return;
        if(right - left - 1 == 7) {
            insertSortQuick(array,left,right);
        }
        int index = midThreeNum(array,left,right);
        swap(array,left,index);
        int par = partition3(array,left,right);
        quick(array,left,par-1);
        quick(array,par+1,right);
    }
    public static void insertSortQuick(int[] array,int left,int right) {
        //遍历整个数组
        for (int i = left + 1; i < array.length; i++) {
            //使0-i有序
            int j = i-1;
            int tmp = array[i];
            for (; j >= left; j--) {
                if(tmp < array[j]) {
                    array[j+1] = array[j];
                }else {
                    //因为前面已经有序，大于之后直接跳出循环
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    //找到left  right 及中间的下标的  中间值
    public static int midThreeNum(int[] array,int left,int right) {
        int mid = (left + right)/2;
        if(array[left] > array[right]) {
            // right  left
            if(array[mid] < array[right]) {
                return right;
            }else if(array[mid] < array[left]) {
                return mid;
            }else {
                return left;
            }
        }else {
            //left   right
            if(array[mid] < array[left]) {
                return left;
            }else if(array[mid] < array[right]) {
                return mid;
            }else {
                return right;
            }
        }
    }

    public static int partition(int[] array,int start,int end) {
        int pivot = array[start];
        int i = start;
        while(start < end) {
            //循环使左边小右边大
            while(start < end && array[end] >= pivot) {
                end--;
            }
            while(start < end && array[start] <= pivot) {
                start++;
            }
            swap(array,start,end);
        }
        //把中间数字换到start
        swap(array,i,start);
        return start;
    }
    //挖坑法
    public static int partition2(int[] array,int start,int end) {
        int pivot = array[start];
        int i = start;
        while(start < end) {
            // 3 5 2 7 4
            while(start < end && array[end] >= pivot) {
                end--;
            }
            array[start] = array[end];
            while(start < end && array[start] <= pivot) {
                start++;
            }
            array[end] = array[start];
        }
        array[start] = pivot;
        return start;
    }
    //快慢指针法
    public static int partition3(int[] array,int start,int end) {
        int prev = start;
        int cur = start+1;
        while(cur <= end) {
           if(array[cur] < array[start] && array[cur] != array[++prev]) {
               swap(array,prev,cur);
           }
           cur++;
        }
        //把最后一个小于[prev]的值换到开头
        swap(array,prev,start);
        return prev;//start不行
    }
    public static void quickSort2(int[] array) {
        int left = 0;
        int right = array.length-1;
        int par = partition3(array,left,right);
        Stack<Integer> stack = new Stack<>();
        if(par > left+1) {
            //left+1是为了保证最少左边右一个元素
            stack.push(left);
            stack.push(par-1);
        }
        if(par < right-1) {
            //right-1是为了
            stack.push(par+1);
            stack.push(right);
        }
        while(!stack.empty()) {
            right = stack.pop();
            left = stack.pop();
            par = partition3(array,left,right);
            if(par > left+1) {
                //left+1是为了保证最少左边右一个元素
                stack.push(left);
                stack.push(par-1);
            }
            if(par < right-1) {
                //right-1是为了
                stack.push(par+1);
                stack.push(right);
            }
        }
    }
    public static void mergeSort1(int[] array) {
        mergeSortFunc(array,0,array.length-1);
    }
    public static void mergeSortFunc(int[] array,int left,int right) {
        if(left == right) {
            return;
        }
        int mid = (left + right) / 2;
        //分解
        mergeSortFunc(array,left,mid);
        mergeSortFunc(array,mid+1,right);
        //合并
        merge(array,left,mid,right);
    }
    public static void merge(int[] array,int left,int mid,int right) {
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2= right;
        int k = 0;
        int[] tmpArray = new int[right - left + 1];
        while(s1 <= e1 && s2 <= e2) {
            if(array[s1] <= array[s2]) {
                tmpArray[k++] = array[s1++];
            }else {
                tmpArray[k++] = array[s2++];
            }
        }
        while(s1 <= e1) {
            tmpArray[k++] = array[s1++];
        }
        while(s2 <= e2){
            tmpArray[k++] = array[s2++];
        }
        for (int i = 0; i < tmpArray.length; i++) {
            array[i + left] = tmpArray[i];
        }
    }
    public static void mergeSort2(int[] array) {
        int gap = 1;
        while(gap < array.length) {
            for (int i = 0; i < array.length; i+=2*gap) {
                int left = i;
                int mid = left + gap - 1;
                int right = mid + gap;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                if(right >= array.length) {
                    right = array.length-1;
                }
                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }
    //计数排序
    public static void countSort(int[] array) {
        int min = array[0];
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            if(array[i] < min) {
                min = array[i];
            }
            if(array[i] > max) {
                max = array[i];
            }
        }
        int len = max - min + 1;
        int[] tmpArray = new int[len];
        //制作计数数组
        for (int i = 0; i < array.length; i++) {
            tmpArray[array[i] - min]++;
        }
        //完成排序
        int Index = 0;
        for (int i = 0; i < len; i++) {
            while(tmpArray[i] != 0) {
                array[Index++] = i + min;
                tmpArray[i]--;
            }
        }
    }
}
